import Vue from "vue";
import VeeValidate, { Validator } from "vee-validate";
import zh_CN from "vee-validate/dist/locale/zh_CN";
// import messages from "assets/js/zh_CN";
Validator.localize({
  zh_CN
});

const config = {
  errorBagName: "errorBags", // 解决和element的校验字段冲突
  fieldsBagName: "fieldBags",
  delay: 0,
  locale: "zh_CN",
  strict: true,
  enableAutoClasses: false,
  classNames: {
    changed: "changed",
    touched: "touched", // the control has been blurred
    untouched: "untouched", // the control hasn't been blurred
    valid: "valid", // model is valid
    invalid: "invalid", // model is invalid
    pristine: "pristine", // control has not been interacted with
    dirty: "dirty" // control has been interacted with
  },
  events: "blur", // 默认input|blur
  inject: true
};

//修改默认错误提示
const dictionary = {
  zh_CN: {
    messages: {
      required: field => "请输入" + field,
      email: () => "邮箱格式不正确",
      numeric: field => field + "只能输入整数"
    },
    attributes: {
      name: "名称"
    }
  }
};

Vue.use(VeeValidate, config);
Validator.localize(dictionary);

Validator.extend("mobile", {
  messages: {
    en: field => field + "必须是11位手机号码"
  },
  validate: value => {
    return (
      value.length == 11 && /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)
    );
  }
});

Validator.extend("phone", {
  messages: {
    zh: "请输入正确固定电话（格式：区号-电话）"
  },
  validate: value => {
    return (
      (value.length === 11 &&
        /^((13|14|15|17|18)[0-9]{1}\d{8})$/.test(value)) ||
      /^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/.test(value)
    );
  }
});

// 版本校验：数字、多个小数点校验，开头必须为数字
Validator.extend("version", {
  getMessage: field => field + "请输入数字 ( 格式1.0.1 )",
  validate: value => {
    return /^\d[.\d]*$/.test(value);
  }
});

// 正负数校验
Validator.extend("numeric", {
  getMessage: field => field + "请输入数字",
  validate: value => {
    return /(^[/\-0-9][0-9]*(.[0-9]+)?)$/.test(value);
  }
});
